﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AddressBook.DAO;
using AddressBook.Entity;
using System.Text;

public partial class GetVersion : System.Web.UI.Page
{
    static VersionNoDAO dao = new VersionNoDAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        ResultEntity result = new ResultEntity();
        result.Name = "VersionNo";

        if (!string.IsNullOrEmpty(Request.QueryString["PhoneType"]) && !string.IsNullOrEmpty(Request.QueryString["LastVersion"]))
        {
            try
            {
                VersionNo versionentity = dao.GetInfo(" where PhoneType = '" + Request.QueryString["PhoneType"] + "' and [Status] = 0 order by PublishTime desc");

                result.ResultCode = "0";
                if (versionentity != null)
                {
                    bool isreturn = false;
                    int lastversion = int.Parse(Request.QueryString["LastVersion"]);
                    switch (versionentity.Operation)
                    {
                        case "=":
                            if (versionentity.LastVersion == lastversion)
                                isreturn = true;
                            break;
                        case ">":
                            if (versionentity.LastVersion > lastversion)
                                isreturn = true;
                            break;
                        case ">=":
                            if (versionentity.LastVersion >= lastversion)
                                isreturn = true;
                            break;
                        case "<":
                            if (versionentity.LastVersion < lastversion)
                                isreturn = true;
                            break;
                        case "<=":
                            if (versionentity.LastVersion <= lastversion)
                                isreturn = true;
                            break;
                    }
                    if (isreturn)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("{");
                        sb.AppendFormat("\"VersionID\":\"{0}\",", versionentity.VersionID);
                        sb.AppendFormat("\"VerName\":\"{0}\",", versionentity.VerName.Replace("\"", ""));
                        sb.AppendFormat("\"VerDesp\":\"{0}\",", versionentity.VerDesp.Replace("\"", ""));
                        sb.AppendFormat("\"DownloadUrl\":\"{0}\",", versionentity.DownloadUrl);
                        sb.AppendFormat("\"PublishTime\":\"{0}\",", versionentity.PublishTime);
                        sb.AppendFormat("\"IsMustUp\":\"{0}\"", versionentity.IsMustUp);
                        sb.Append("}");
                        result.Contents = sb.ToString();
                    }
                }
            }
            catch
            {
                result.ResultCode = "-2";
                result.Desc = "接口出错！";
            }
        }
        else
        {
            result.ResultCode = "-2";
            result.Desc = "参数不正确！";
        }
        Response.Write(Common.ReturnResult(result));
    }
}